//
//  NewsScrollView.m
//  口袋
//
//  Created by dllo on 15/5/19.
//  Copyright (c) 2015年 口袋. All rights reserved.
//

#import "NewsScrollView.h"
#import "HeaderModel.h"
#import "ConnectModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SANetWorkingTask.h"
#import "DKNightVersion.h"

#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface NewsScrollView ()<UIScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray * array;

@property (nonatomic, retain) UIScrollView * scroll;

@end

@implementation NewsScrollView

- (void)dealloc
{
    [_array release];
    [_scroll release];
    [_pageCon release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{

    self = [super initWithFrame:frame];
    if (self) {
        self.array = [NSMutableArray array];
        
        self.page = 1;
        self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        _scroll.delegate = self;
        _scroll.pagingEnabled = YES;
        _scroll.showsHorizontalScrollIndicator = NO;
        _scroll.nightBackgroundColor = UIColorFromRGB(0x343434);
        
        self.pageCon = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH - 50, HEIGHT - 30, 30, 20)];
        _pageCon.currentPageIndicatorTintColor = [UIColor blackColor];
        _pageCon.pageIndicatorTintColor = [UIColor lightGrayColor];
        _pageCon.numberOfPages = 4;
        _pageCon.nightBackgroundColor = UIColorFromRGB(0x343434);
        [self addSubview:_pageCon];
        [_pageCon release];
        
        [self startConnect];

        [self addSubview:_scroll];
        [_scroll release];
    }
    return self;
}

// 数据请求
- (void)startConnect
{
    [SANetWorkingTask request:@"http://c.3g.163.com/nc/ad/headline/0-4.html" parmater:nil block:^(NSDictionary *result) {
        NSArray * array = [result objectForKey:@"headline_ad"];
        for (NSDictionary * dic in array) {
            HeaderModel * model = [[HeaderModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
            [model release];
        }
        [self setScrollImage];
    }];
}

// scroll界面
- (void)setScrollImage
{
    // 图片无限滑动的数组
    NSMutableArray * imageArray = [NSMutableArray array];
    for (int i = 0; i < 4; i++) {
        HeaderModel * model = [self.array objectAtIndex:i];
        [imageArray addObject:model];
    }
    
    // 添加第一张最后一张到数组
    id imageFirst = [imageArray firstObject];
    id imageLast = [imageArray lastObject];
    [imageArray addObject:imageFirst];
    [imageArray insertObject:imageLast atIndex:0];
    
    // scroll赋值
    for (int i = 0; i < imageArray.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i , 0, WIDTH, HEIGHT - 40)];
//        imageView.nightBackgroundColor = UIColorFromRGB(0x343434);
        
        HeaderModel * model = [imageArray objectAtIndex:i];
        NSURL * url = [NSURL URLWithString:model.imgsrc];
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
        imageView.tag = i + 1;
        [_scroll addSubview:imageView];
        [imageView release];
        
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * i + 10, HEIGHT - 40, WIDTH - 50, 40)];
        label.text = model.title;
//        label.nightBackgroundColor = UIColorFromRGB(0x343434);
        label.nightTextColor = [UIColor lightGrayColor];
        [_scroll addSubview:label];
        [label release];
        
        // 添加点击事件
        [imageView setUserInteractionEnabled:YES];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        [tap release];
        
    }
    _scroll.contentSize = CGSizeMake(WIDTH * imageArray.count, HEIGHT - 40);
    [_scroll setContentOffset:CGPointMake(_scroll.frame.size.width, 0)];
}

// tapAction点击事件
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    UIImageView * imageV = (UIImageView *)tap.view;
    NSInteger index = 0;
    if (imageV.tag == 1) {
        index = 3;
    } else if (imageV.tag > 1 && imageV.tag < 6) {
        index = imageV.tag - 2;
    } else {
        index = 0;
    }
    HeaderModel * model = [self.array objectAtIndex:index];
    NSString * path = model.url;
    NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithObject:path forKey:@"url"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RECEIVE" object:self userInfo:dic];
}


#pragma mark -ScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / WIDTH - 1;
    self.pageCon.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 判断是否为第一张和最后一张,如果是第一张,把第一张偏移量设置到最后,如果是最后一张,把最后一张偏移量设置到第一张
    CGPoint offSet = scrollView.contentOffset;
    CGFloat width = scrollView.frame.size.width;
    if (offSet.x == (_scroll.contentSize.width - width)) {
        [scrollView setContentOffset:CGPointMake(width, 0)];
    } else if (offSet.x == 0) {
        [scrollView setContentOffset:CGPointMake(_scroll.contentSize.width - width * 2, 0)];
    }
}


@end
